package com.chinasoft04.mall.service.impl;

import com.chinasoft04.mall.mapper.OrderMapper;
import com.chinasoft04.mall.service.OrderService;
import com.chinasoft04.mall.dto.Order;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class OrderServiceImpl implements OrderService {

    OrderMapper orderMapper;

    @Override
    public Object getOrders(@Nullable String query, Integer pagenum, Integer pagesize) {

        Map<String, Object> map = new HashMap<>();

        PageHelper.startPage(pagenum, pagesize);
        List<Order> orders = orderMapper.selectAll();

        if (query != null && !query.isEmpty()){
            PageHelper.startPage(pagenum, pagesize);
            orders = orderMapper.query(query);
        }
        if (orders != null) {
            PageInfo<Order> info = new PageInfo<>(orders);
            map.put("total", info.getTotal());
            map.put("pagenum", pagenum.toString());
            map.put("goods", orders);
            return map;
        }
        return null;
    }
}
